package com.tsinghua.discharge.service.impl;

import com.tsinghua.discharge.common.Const;
import com.tsinghua.discharge.common.ServerResponse;
import com.tsinghua.discharge.dao.BaseDataMapper;
import com.tsinghua.discharge.dao.DataDateMapper;
import com.tsinghua.discharge.dao.RegionMappingMapper;
import com.tsinghua.discharge.dao.SectorMapper;
import com.tsinghua.discharge.model.User;
import com.tsinghua.discharge.service.ConditionSelectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class ConditionSelectServiceImpl implements ConditionSelectService {
    @Autowired
    SectorMapper sectorMapper;
    @Autowired
    RegionMappingMapper regionMappingMapper;
    @Autowired
    DataDateMapper dataDateMapper;
    @Autowired
    BaseDataMapper baseDataMapper;
    // 查询顶级部门
    public ServerResponse supSectorSelect(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        try{
            if(user == null){
                return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        List<String> supsectorNums = sectorMapper.supSectorSelect(user.getId());
        if(supsectorNums.size() == 0){
            return ServerResponse.createBySuccessMessage("部门数据为空");
        }else{
            return ServerResponse.createBySuccess(supsectorNums);
        }

    }
    // 查询二级部门
    public ServerResponse sectorSelect(String supSector,HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        try{
            if(user == null){
                return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        if(supSector != null) {
            Integer sectorid = sectorMapper.selectBySecName(supSector, user.getId());
            List<String> sectors = sectorMapper.selectBySupsecid(sectorid,user.getId());
            if(sectors.size() == 0){
                return ServerResponse.createBySuccessMessage("部门数据为空");
            }else{
                return ServerResponse.createBySuccess(sectors);
            }
        }else{
            return ServerResponse.createByErrorMessage("上级部门不能为空");
        }
    }
    //查询service部门
    public ServerResponse serviceSelect(String sector,HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        try{
            if(user == null){
                return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        if(sector != null) {
            Integer serviceid = sectorMapper.selectBySecName(sector,user.getId());
            List<String> services = sectorMapper.selectBySupsecid(serviceid,user.getId());
            if(services.size() == 0){
                return ServerResponse.createBySuccessMessage("部门数据为空");
            }else{
                return ServerResponse.createBySuccess(services);
            }
        }else{
            return ServerResponse.createByErrorMessage("上级部门不能为空");
        }
    }
    // 查询technology部门
    public ServerResponse technologySelect(String service,HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        try{
            if(user == null){
                return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        if(service != null) {
            Integer technologyid = sectorMapper.selectBySecName(service,user.getId());
            List<String> technologys = sectorMapper.selectBySupsecid(technologyid,user.getId());
            if(technologys.size() == 0){
                return ServerResponse.createBySuccessMessage("部门数据为空");
            }else{
                return ServerResponse.createBySuccess(technologys);
            }
        }else{
            return ServerResponse.createByErrorMessage("上级部门不能为空");
        }
    }
    //查询地区数据
    public ServerResponse regionSelect(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        try{
            if(user == null){
                return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        List<String> regions = regionMappingMapper.selectBulk(user.getId());
        if(regions.size() == 0){
            return ServerResponse.createBySuccessMessage("地区数据为空");
        }else{
            return ServerResponse.createBySuccess(regions);
        }
    }
    //查询日期数据
    public ServerResponse dateSelect(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        try{
            if(user == null){
                return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        List<String> dates = dataDateMapper.selectBulk(user.getId());
        if(dates.size() == 0){
            return ServerResponse.createBySuccessMessage(" 日期数据为空");
        }else{
            return ServerResponse.createBySuccess(dates);
        }
    }
    //查询scenario
    public ServerResponse scenarioSelect(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        try{
            if(user == null){
                return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        List<String> scenarios = baseDataMapper.scenarioSelect(user.getId());
        if(scenarios.size() == 0){
            return ServerResponse.createBySuccessMessage(" 情景数据为空");
        }else{
            return ServerResponse.createBySuccess(scenarios);
        }
    }
    //查询fuel
    public ServerResponse fuelSelect(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        try{
            if(user == null){
                return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        List<String> fuels = baseDataMapper.fuelSelect(user.getId());
        if(fuels.size() == 0){
            return ServerResponse.createBySuccessMessage(" 燃料数据为空");
        }else{
            return ServerResponse.createBySuccess(fuels);
        }
    }



}
